package cn.momoshenchi.medicine.controller;

import cn.momoshenchi.medicine.pojo.BaseAccout;
import cn.momoshenchi.medicine.pojo.BaseDoctor;
import cn.momoshenchi.medicine.pojo.BaseDrug;
import cn.momoshenchi.medicine.pojo.ConsultAsk;
import cn.momoshenchi.medicine.service.BaseAccoutService;
import cn.momoshenchi.medicine.service.BaseDoctorService;
import cn.momoshenchi.medicine.service.ConsultAskService;
import cn.momoshenchi.medicine.util.AssertUtil;
import cn.momoshenchi.medicine.util.PageUtil;
import cn.momoshenchi.medicine.util.ResultInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/7/13 - 13:55
 */
@RestController()
@RequestMapping("/doctor")
public class DoctorController
{
    @Autowired
    private BaseAccoutService baseAccoutService;

    @Autowired
    private BaseDoctorService baseDoctorService;


    @Autowired
    private ConsultAskService consultAskService;
    /**
     * 医生第一次登录
     * @param baseAccout
     */
    @PostMapping("/login")
    public ResultInfo login(@RequestBody BaseAccout baseAccout)
    {
        BaseAccout accout = baseAccoutService.getById(baseAccout.getUserId());
        if(accout==null)
        {
            baseAccout.setUserType("2");
            AssertUtil.isTrue(!baseAccoutService.save(baseAccout),"登录失败");

        }
        return new ResultInfo();
    }

    /**
     * 分页查询医生数据
     * @return
     */
    @GetMapping("/page")
    public ResultInfo list( PageUtil page)
    {
        IPage<BaseDoctor> doctorPage =new Page<>(page.getPage(), page.getSize());
        doctorPage = baseDoctorService.page(doctorPage);
        return new ResultInfo(doctorPage);

    }
    /**
     * 分页,查询医生复诊配药记录
     * @return
     */
    @PostMapping("/consult/page")
    public ResultInfo list(@RequestBody Map<String, Object> map)
    {
        String doctorId= (String) map.get("doctorId");
        String status= (String) map.get("status");
        AssertUtil.isTrue(doctorId==null,"医生id不能为空");
        AssertUtil.isTrue(status==null,"状态不能为空");
        Integer page= (Integer) map.get("page");
        Integer size= (Integer) map.get("size");
        IPage<ConsultAsk> consultPage =null;
        if(page!=null&&size!=null)
        {
            consultPage=new Page<>(page, size);
        }
        else
        {
            consultPage=new Page<>(1, 5);
        }
        consultPage= consultAskService.page(consultPage,new QueryWrapper<ConsultAsk>().eq("doctor_id", doctorId).eq("consult_status",status));
        return new ResultInfo(consultPage);

    }

    /**
     * 医生web端登录
     * @param  map 医生id
     */
    @PostMapping("/web/login")
    public ResultInfo login(@RequestBody Map<String, Object> map)
    {
        String doctorId= (String) map.get("doctorId");
        AssertUtil.isTrue(doctorId==null,"医生id不能为空");
        BaseDoctor baseDoctor = baseDoctorService.getById(doctorId);
        return new ResultInfo(baseDoctor);
    }

}
